package lumis.service.pagepersonalization.doui.postloadprocessor;

import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.preferences.IPreferences;
import lumis.portal.serviceinstance.acl.ServiceInstancePermissions;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.service.pagepersonalization.PagePersonalizationUtil;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration.Type;
import lumis.util.ITransaction;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Node;

/**
 * Post load processor responsible for indicating which widgets are allowed to
 * be dragged into the personalized page.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class WidgetListPostLoadProcessor implements IPostLoadProcessor
{
	@SuppressWarnings("unchecked")
	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		if (!(source instanceof TabularSource))
		{
			return;
		}

		Collection<String> interfaceInstanceIds = ManagerFactory.getServiceInterfaceInstanceManager().getIdsByPageId(sessionConfig, source.getDouiContext().getRequest().getPageConfig().getId(),
				transaction);
		List<String> usedIds = new ArrayList<String>(50);
		for (String interfaceInstanceId : interfaceInstanceIds)
		{
			ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, interfaceInstanceId, transaction);
			if (serviceInterfaceInstanceConfig.getServiceInterfaceId().equals(source.getDouiContext().getRequest().getServiceConfig().getId() + ".widgetHolder"))
			{
				if (!PagePersonalizationUtil.inEditMode(source.getDouiContext().getRenderRequest()))
				{
					IPreferences preferences = ManagerFactory.getPreferencesManager().getPreferences(sessionConfig, serviceInterfaceInstanceConfig.getId(),
							IPreferences.TYPE_SERVICE_INTERFACE_INSTANCE);

					String[] widgets = preferences.getValues(PagePersonalizationUtil.WIDGETS_PREFERENCES_NAME, PagePersonalizationUtil.getDefaultWidgets(sessionConfig, interfaceInstanceId, source
							.getDouiContext().getRenderRequest(), transaction));

					Collections.addAll(usedIds, widgets);
				}
				else
				{
					PagePersonalizationHolderConfiguration temp = PagePersonalizationHolderConfiguration.getByTypeAndHolderInstanceId(Type.Temporary, interfaceInstanceId);
					if (temp != null)
						Collections.addAll(usedIds, temp.expand());
				}
			}
		}

		TabularData data = ((TabularSource<?>) source).getData();
		for (Iterator<ISourceData> it = data.getRows().iterator(); it.hasNext();)
		{
			ISourceData row = it.next();

			// remove widgets with II that user doesn't have access
			String interfaceInstanceId = row.get("interfaceInstanceId", String.class);
			ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, interfaceInstanceId, transaction);
			String serviceId = serviceInterfaceInstanceConfig.getServiceInterfaceId().substring(0, serviceInterfaceInstanceConfig.getServiceInterfaceId().lastIndexOf('.'));
			ServiceInstancePermissions permissions = ManagerFactory.getServiceInstanceAclManager().getPermissions(sessionConfig, serviceId, transaction);
			if (!ManagerFactory.getServiceInstanceAclManager().checkPermission(sessionConfig, serviceInterfaceInstanceConfig.getServiceInstanceId(), permissions.VIEW_SERVICE_INSTANCE, transaction))
			{
				it.remove();
				continue;
			}

			row.put("liId", "OffWid" + interfaceInstanceId);
			String id = row.get("contentId", String.class);
			if (usedIds.contains(id))
			{
				row.put("liClass", "cLumPPOfferedWidget cLumPPUndraggable cLumPPDraggable_" + source.getDouiContext().getRequest().getServiceInstanceConfig().getId());
			}
			else
			{
				row.put("liClass", "cLumPPOfferedWidget cLumPPDraggable_" + source.getDouiContext().getRequest().getServiceInstanceConfig().getId());
			}
		}
	}
}
